﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSSystem.BLL;
using MyGeneration.dOOdads;
using System.Collections;

namespace WebUI.EditPages
{
    public partial class EditExpense : BasePage
    {
        POSSystem.BLL.Expenses expense;

        int expID;
        string mode;

        protected void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);

            if (Request.QueryString[MODE] == null)
            {
                CloseDlg(0);
                return;
            }

            mode = Request.QueryString[MODE];

            if (Request.QueryString[ID] == null && (mode == EDIT_MODE || mode == VIEW_MODE))
            {
                CloseDlg(0);
                return;
            }

            expID = Request.QueryString[ID] != null ? int.Parse(Request.QueryString[ID]) : 0;
            if (mode == ADD_MODE)
                btnSave.Enabled = false;

            if (!IsPostBack && mode != ADD_MODE)
            {
                LoadInfo();
            }
            else if (!IsPostBack)
                editExpense.SpentDate = DateTime.UtcNow.AddHours(SysVars.TimeZone);
        }

        public void btnSave_OnClick(Object sender, EventArgs e)
        {
            Save();
            ScriptManager.RegisterClientScriptBlock(this, typeof(Page), Guid.NewGuid().ToString(), String.Format("window.returnValue={0};", 1), true);
        }

        private bool Save()
        {
            TransactionMgr tx = TransactionMgr.ThreadTransactionMgr();

            expense = new POSSystem.BLL.Expenses();
            string oldFilter = expense.Filter;
            try
            {
                tx.BeginTransaction();
                if (mode == ADD_MODE)
                {
                    expense.AddNew();
                    expense.Deleted = false;
                    expense.BranchID = Branches.CurrentBranchID;
                }
                else
                {
                    expense.LoadByPrimaryKey(expID);
                }
                expense.PaymentMethodID = editExpense.PaymentMethodID;
                expense.LastModifiedByUser = SessionUser.ID;
                expense.LastModifiedDate = DateTime.UtcNow.AddHours(SysVars.TimeZone);
                expense.Title = editExpense.Title;
                expense.Amount = editExpense.Amount;
                expense.InvoiceNo = editExpense.InvoiceNo;
                expense.Purchaser = editExpense.Purchaser;
                expense.Description = editExpense.Description;
                expense.SpentDate = editExpense.SpentDate;
                expense.Save();

                tx.CommitTransaction();


                ((Popup)Master).ShowMessage("Saved");
                return true;
            }
            catch (Exception ex)
            {
                expense.RejectChanges();
                expense.Filter = oldFilter;
                tx.RollbackTransaction();
                ((Popup)Master).ShowErrorMessage(ex.Message);
                ErrorLog.WriteToFile("editExpense.aspx.cs", "Save", ex.Message + "\r\n" + ex.StackTrace);
                return false;
            }
        }

        private void LoadInfo()
        {
            try
            {
                expense = new POSSystem.BLL.Expenses();
                expense.LoadByPrimaryKey(expID);

                if (!expense.IsColumnNull(POSSystem.BLL.Expenses.ColumnNames.Title))
                    editExpense.Title = expense.Title;
                if (!expense.IsColumnNull(POSSystem.BLL.Expenses.ColumnNames.Amount))
                    editExpense.Amount = expense.Amount;
                if (!expense.IsColumnNull(POSSystem.BLL.Expenses.ColumnNames.Description))
                    editExpense.Description = expense.Description;
                if (!expense.IsColumnNull(POSSystem.BLL.Expenses.ColumnNames.InvoiceNo))
                    editExpense.InvoiceNo = expense.InvoiceNo;
                if (!expense.IsColumnNull(POSSystem.BLL.Expenses.ColumnNames.Purchaser))
                    editExpense.Purchaser = expense.Purchaser;
                if (!expense.IsColumnNull(POSSystem.BLL.Expenses.ColumnNames.SpentDate))
                    editExpense.SpentDate = expense.SpentDate;
                if (!expense.IsColumnNull(POSSystem.BLL.Expenses.ColumnNames.PaymentMethodID))
                    editExpense.PaymentMethodID = expense.PaymentMethodID;
            }
            catch (Exception ex)
            {
                ((Popup)Master).ShowErrorMessage(ex.Message);
                ErrorLog.WriteToFile("editExpense.aspx.cs", "LoadInfo", ex.StackTrace);
            }
        }

        public void btnSaveAndClose_OnClick(Object sender, EventArgs e)
        {
            if (Save())
                CloseDlg(1);
        }
    }
}
